//
//  DetaildViewController.m
//  Birthday Reminder
//
//  Created by Andrei Marcu on 10/24/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DetailedViewController.h"

@interface DetailedViewController (Private)

- (void)addNotification;
- (void)setDatePicker;
- (void)saveSettings;
- (void)deleteNotification;

@end

@implementation DetailedViewController

@synthesize item;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        customNotification = nil;
    }
    return self;
}

- (void)dealloc
{
    self.item = nil;
    [notificationDateLabel release];
    [datePicker release];
    [notificationSwitch release];
    [customNotification release];
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.navigationItem.title = self.item.name;
    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveSettings)];
    self.navigationItem.rightBarButtonItem = saveButton;
    [saveButton release];
    [self setDatePicker];
}

- (void)viewDidUnload
{
    [notificationDateLabel release];
    notificationDateLabel = nil;
    [datePicker release];
    datePicker = nil;
    [notificationSwitch release];
    notificationSwitch = nil;
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)changenotificationDateLabel:(id)sender {
    NSDateFormatter *formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"dd MMMM yyyy HH:mm"];
    notificationDateLabel.text = [formater stringFromDate:[datePicker date]];
    [formater release];
}

#pragma mark - Private methods 
- (void)setDatePicker {
    NSArray *notifications = [UIApplication sharedApplication].scheduledLocalNotifications;
    NSDate *notificationDate = item.nextBirthday;
    notificationSwitch.on = NO;
    for (UILocalNotification *notification in notifications) {
        if ([notification.userInfo objectForKey:item.recordID]) {
            //if custom notification is found
            customNotification = [notification retain];
            notificationDate = notification.fireDate;
            notificationSwitch.on = YES;
            break;
        }
    }
    if (notificationDate != nil) {
        datePicker.date = notificationDate;
        [self changenotificationDateLabel:nil];
    }
}

- (void)deleteNotification {
    if (customNotification) {
        [[UIApplication sharedApplication] cancelLocalNotification:customNotification];
        [customNotification release];
    }
}

- (void)addNotification {
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
      
    localNotif.fireDate = [datePicker date];
    localNotif.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    
    // Notification details
    localNotif.alertBody = [NSString stringWithFormat:@"%@'s birthday is in %@", item.name, item.birthday];
    // Set the action button
    localNotif.alertAction = @"View";
    
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 1;
    
    //	// Specify custom data for the notification
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:item.recordID];
    localNotif.userInfo = infoDict;
    
    //set the repeat rate
    localNotif.repeatInterval = NSYearCalendarUnit;
    
    // Schedule the notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    [localNotif release];
}

- (void)saveSettings {
    if (notificationSwitch.on == YES) {
        [self addNotification];
    } else {
        [self deleteNotification];
    }
}
@end
